/*
 * $Id: OldWorldOfDarknessRoller.java 12 2009-12-10 04:46:25Z dave.terhune $
 *
 * Copyright (C) 2009 David Terhune
 *
 * This file is part of DiceRoller.
 *
 * DiceRoller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DiceRoller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DiceRoller.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.dterhune.gaming.dice;

import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.Random;

/** Rolls dice for the old World of Darkness. */
@SuppressWarnings("serial") // Never actually serialized, so ignore warning.
class OldWorldOfDarknessRoller extends Random implements View.OnClickListener
{
    /** Number of items to print per line. */
    private static final int ITEMS_PER_LINE = 10;

    /** Result text box. */
    private TextView result;
    /** Successes text box. */
    private TextView successes;
    /** Difficulty spinner. */
    private Spinner difficulty;
    /** Subtract 1s check box. */
    private CheckBox subtract1s;
    /** Reroll 10s check box. */
    private CheckBox reroll10s;

    /**
     * Creates a new OldWorldOfDarknessRoller.
     * @param activity The parent DiceRoller
     */
    public OldWorldOfDarknessRoller(Activity activity)
    {
        // Set the click handler
        View b = activity.findViewById(R.id.odp1);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp2);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp3);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp4);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp5);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp6);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp7);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp8);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp9);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp10);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp11);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp12);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp13);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp14);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp15);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp16);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp17);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp18);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp19);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.odp20);
        b.setOnClickListener(this);

        // Cache the text fields
        result = (TextView)activity.findViewById(R.id.owod_result);
        successes = (TextView)activity.findViewById(R.id.owod_success);

        // Difficulty spinner
        difficulty = (Spinner)activity.findViewById(R.id.owod_diff);
        difficulty.setSelection(4); // 6 is at index 4 in the list

        // Check boxes
        subtract1s = (CheckBox)activity.findViewById(R.id.owod_subtract1);
        reroll10s = (CheckBox)activity.findViewById(R.id.owod_reroll10);
    }

    /**
     * Click handler.
     * @param v view that was clicked
     */
    public void onClick(View v)
    {
        // Find out how big the pool is
        int pool = 0;
        switch(v.getId())
        {
            case R.id.odp1:  pool =  1; break;
            case R.id.odp2:  pool =  2; break;
            case R.id.odp3:  pool =  3; break;
            case R.id.odp4:  pool =  4; break;
            case R.id.odp5:  pool =  5; break;
            case R.id.odp6:  pool =  6; break;
            case R.id.odp7:  pool =  7; break;
            case R.id.odp8:  pool =  8; break;
            case R.id.odp9:  pool =  9; break;
            case R.id.odp10: pool = 10; break;
            case R.id.odp11: pool = 11; break;
            case R.id.odp12: pool = 12; break;
            case R.id.odp13: pool = 13; break;
            case R.id.odp14: pool = 14; break;
            case R.id.odp15: pool = 15; break;
            case R.id.odp16: pool = 16; break;
            case R.id.odp17: pool = 17; break;
            case R.id.odp18: pool = 18; break;
            case R.id.odp19: pool = 19; break;
            case R.id.odp20: pool = 20; break;
        }

        // Get the difficulty, capping it between 2 and 10
        int diff = Integer.parseInt(difficulty.getSelectedItem().toString());

        // See whether to reroll 10s
        boolean reroll = reroll10s.isChecked();
        boolean subtract = subtract1s.isChecked();

        int succ = 0;
        StringBuilder sb = new StringBuilder();

        // Roll the dice (adding an extra die to the pool for every 10 rolled,
        // if appropriate).
        for(int i = 0; i < pool; i++)
        {
            // Line wrap or add comma
            if(i != 0 && (i % ITEMS_PER_LINE) == 0)
            {
                sb.append('\n');
            }
            else if(i != 0)
            {
                sb.append(", ");
            }

            // Roll
            int die = nextInt(10) + 1;
            sb.append(die);

            // Count successes
            if(subtract && die == 1)
            {
                succ--;
            }
            else if(die >= diff)
            {
                succ++;
            }

            // Add another die to the pool
            if(reroll && die == 10)
            {
                pool++;
            }
        }

        // Print results
        result.setText(sb);
        if(succ < 0)
        {
            successes.setText("Botch (" + succ + ")");
        }
        else
        {
            successes.setText(Integer.toString(succ));
        }
    }
}
